module WindSpirit
{
    export class ScoreLayer extends WindSpirit.MarskLayer
    {
        public padding = 5;
        public titlePng: egret.Bitmap;
        public scorePng: egret.Bitmap;
        public scoreLabel: egret.TextField;
        public rank: egret.Bitmap;
        
        public btnLink: WindSpirit.IButton;//链接公众号按钮
        public btnRank: WindSpirit.IButton;
        public btnRetry: WindSpirit.IButton;
        
        public record: egret.TextField;
        public score;
        public retryFunc:Function;
        public EVENT_RETRY = "EVENT_RETRY";
        public constructor(score:number,thisObj:any,retryFunc)
        {
            super();
            this.score = score;
            this.retryFunc = retryFunc;
            this.addEventListener(this.EVENT_RETRY,this.retryFunc,thisObj);
            if(score > WindSpirit.GameData.record)
            {
                WindSpirit.GameData.record = score;
                WindSpirit.Net.uploadScore(score);
            }
            var title = new egret.Bitmap(RES.getRes("over_png"));
            title.anchorX = 0.5;
            title.x = this.container.width >> 1;
            title.y = 55;
            this.titlePng = title;
            //this.container.scaleX = WindSpirit.Util.scaleX;
            //this.container.scaleY = WindSpirit.Util.scaleY;
            this.container.addChild(title);
            
            var scorePng = new egret.Bitmap(RES.getRes("score_png"));
            scorePng.anchorX = 0.5;
            scorePng.x = this.container.width >> 1;
            scorePng.y = 210;
            this.scorePng = scorePng;
            this.container.addChild(scorePng);
            
            var scoreLabel = new egret.TextField;
            scoreLabel.anchorX = 0.5;
            scoreLabel.size = 150;
            scoreLabel.x = this.container.width >> 1;
            scoreLabel.text = this.score.toString();
            scoreLabel.y = 250;
            this.scoreLabel = scoreLabel;
            this.container.addChild(scoreLabel);
            
            var record = new egret.TextField;
            record.size = 30;
            record.fontFamily = WindSpirit.Const.FONT_FAMILY;
            record.anchorX = 0.5;
            record.x = this.container.width >> 1;
            record.y = scoreLabel.y + scoreLabel.height+this.padding;
            record.text = "最高纪录"+WindSpirit.GameData.record+"分";
            this.record = record;
            this.container.addChild(record);
            
            var btnRank = new WindSpirit.IButton("排行榜");
            btnRank.anchorX = 0.5;
            btnRank.x = this.container.width >> 1;
            btnRank.y = 477;
            this.btnRank = btnRank;
            btnRank.addEventListener(egret.TouchEvent.TOUCH_TAP,this.showRank,this);
            this.container.addChild(btnRank);
            
            var btnShare = new WindSpirit.IButton("关注我们");
            btnShare.anchorX = 0;
            btnShare.y = 613;
            btnShare.x = this.padding;
            this.btnLink = btnShare;
            btnShare.addEventListener(egret.TouchEvent.TOUCH_TAP,this.btnLinkClick,this);
            this.container.addChild(btnShare);
            
            var retry = new WindSpirit.IButton("重新游戏");
            retry.anchorX = 1;
            retry.x = this.container.width - this.padding;
            retry.y = btnShare.y;
            this.btnRetry = retry;
            retry.touchEnabled = true;
            retry.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.doRetry,this);
            this.container.addChild(retry);
        }
        public btnLinkClick()
        {
            this.addChild(new WindSpirit.SnsLayer);
        }
        public doRetry()
        {
            this.btnRetry.touchEnabled = false;
            console.log("doretry");
            this.parent.removeChild(this);
            this.dispatchEventWith(this.EVENT_RETRY);
        }
        public showRank()
        {
            var parent = this.parent;
            //thisObj.removeChild(this);
            parent.addChild(new WindSpirit.RankLayer);
        }
    }
}